package lab_2;

import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.JMSException;
import javax.jms.MessageFormatException;

/**
 *
 * @author grp02 - Pascal Charreix and Meike Paetz
 *
 * The Listener class which implements the MessageListener, represents the
 * Message Listener.
 */
public class Listener implements MessageListener {

    private SmartToursClient STClient;

    /**
     *
     * Listener Constructor
     */
    public Listener(SmartToursClient smartToursClient) {
        STClient = smartToursClient;
    }

    /**
     * 
     * Casts an incoming message to an ObjectMessage.
     * This will be displayed.
     */
    public void onMessage(Message message) {
        try {
            if (message instanceof ObjectMessage) {
                Invoice invoice;
                ObjectMessage objectMessage = (ObjectMessage) message;
                invoice = (Invoice) objectMessage.getObject();
                System.out.println("Reading object message: " + invoice.getInvoiceInformation());
                STClient.sendServiceOperatorMessage("Package paid");
                STClient.sendServiceOperatorPaymentCallBack(invoice);
                STClient.stop();
            } else {
                System.err.println("Message is not a TextMessage/ObjectMessage");
            }
        } catch (MessageFormatException e) {
            System.err.println("Object deserialization failed: " + e.toString());
        } catch (JMSException e) {
            System.err.println("JMS exception in onMessage(): " + e.toString());
        } catch (Throwable t) {
            System.err.println("Exception in onMessage():" + t.getMessage());
        }
    }
}
